package com.m3u8.db;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import androidx.room.Update;

import com.m3u8.db.model.M3u8Model;

import java.util.List;

/**
 * M3u8的DAO层
 */
@Dao
public abstract class M3u8Dao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    public abstract long[] insert(M3u8Model... m3u8Models);

    @Update
    public abstract int update(M3u8Model... m3u8Models);

    @Delete
    public abstract int delete(M3u8Model... m3u8Models);

    @Query("DELETE FROM tb_m3u8 WHERE id = :m3u8Id")
    public abstract int delete(long m3u8Id);

    @Query("SELECT * FROM tb_m3u8")
    public abstract List<M3u8Model> queryForAll();

    @Query("SELECT * FROM tb_m3u8 WHERE url = :m3u8url")
    public abstract List<M3u8Model> queryAllByUrl(String m3u8url);

    @Query("SELECT * FROM tb_m3u8 WHERE id = :m3u8Id")
    public abstract List<M3u8Model> queryAllById(long m3u8Id);

    @Query("UPDATE tb_m3u8 SET hasDownSize = hasDownSize + 1 , currentTs = :currentTs WHERE id = :m3u8Id")
    public abstract int updateM3u8TsInfo(long m3u8Id, String currentTs);

    @Query("UPDATE tb_m3u8 SET state = :state WHERE id = :m3u8Id")
    public abstract int updateM3u8State(int state, long m3u8Id);

    @Query("SELECT * FROM tb_m3u8 WHERE  state != :state")
    public abstract List<M3u8Model> getM3u8NotSuccess(int state);
}
